Quick Index
Overview
The Express web framework and EJS templating engine are two powerful pieces of the Node.js web programming stack. The goal of this project is to get familiar with them.
Before Starting
Before starting this project, if you want to ensure your understanding of a URL relative to web programming, then go here...
Guide Examples for Project
Notes:
The "Book Reference" is "Get Programming with Node.js (2019, Jonathan Wexler)" Web searches on these subjects will produce generous listings
We'll be creating source files with different extensions ("js", "ejb", etc). These are all plain text files . Here is a note on saving with different extensions .
Also, here is a link to Assignment "Node1" (Core Concepts Drills) .
Problem Approach
It is coder's choice as to how the problem is approached, -- you only need to complete one project. The step-by-step examples allow for incremental learning. But you do not need to hand in five projects. Just the one listed in "Project Specs" below.
Many of the terms used here will be new. They are explained in the examples.
Please feel free to customize any of the greetings and web page content used in the examples for your project!
Project Specs
Project directory: express-templates Web Server Port: 3000
Create a Web Server
Create a web server using the Express web framework.
Use Controllers
Put the callback function code for the routes into a controller, in a file with this relative project path:
controllers/home-controller.js
📋
Add Middleware
Add middleware using the Express framework support for middleware.
The middleware should log (print) the request url path to the console. E.g., something like:
request received in middleware
url path and query: /items/beets
📋
Support Multiple Routes
Support three or more routes on the web server. Each route should have its own controller (callback) function (all in file "controllers/home-controller.js")
Note that the next section "Use EJS Template Engine" provides info pertinent to this section.
We'll now describe the three routes to code:
Home Route
A home URL "http://localhost:3000/" should produce a web browser page like:
Greetings from the web server
Thu Mar 25 2021 14:03:47 GMT-0500 (Central Daylight Time)
📋
Here is the js to get a date and time string:
(new Date()).toString()
📋
Even better (optional but good fun):
Show date and time separately with nice formatting using Node.js public module "date-and-time" See API section on this page: date-and-time (it would need to be installed using npm)
Vegetable Name Route
The route "items/<vegetable-name>"
📋
uses a dynamic parameter "vegetable-name". E.g., URL "http://localhost:3000/items/kale" should produce a web browser page like:
Yummy kale!
Plus any HTML to beautify the web page (coder's choice)
plus show more dynamic content like date, etc (coder's choice)
📋
URL "http://localhost:3000/items/beets" should produce a web browser page like:
Just like "kale" above except for this difference:
Yummy beets!
...
Coder's Choice Route
Add another web page for a route with a dynamic parameter similar to "items/<vegetable-name>"
📋
above
Use EJS Template Engine
The web server should use the EJS templating engine to produce the web pages described in the previous "routing" section.
Put EJS (extension ".ejs") files into project sub-directory "views".
The EJS Files:
URL Description Project Relative File Path
home route home page views/index.ejs
vegetable name route veggie page views/veg-page.ejs
coder's choice route coder's choice coder's choice
Submitting
Before archiving, please delete all project "node_modules" sub-directories (if/when present). Here is why... Create a ZIP archive file named in the normal way Copy the project directory "express-templates" into the archive Please do not nest archives
CHAP802 <
Express and EJS Project
> CHAP804